package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouRoundConfig;
import com.yanqu.road.entity.enums.activity.jiuzhou.eJiuZhouGameZoneType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;

@Cmd(code = GameProtocol.S_JIU_ZHOU_SUPPORT_MAIL_REWARD_WITH_BATTLE_FROM_CROSS, desc = "九州邮件参赛者奖励")
public class JiuZhouSupportMailRewardWithBattleFromCrossCmd implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            return;
        }

        CrossJiuZhouProto.CrossJiuZhouSupportMailRewardWithBattleMsg reqMsg = CrossJiuZhouProto.CrossJiuZhouSupportMailRewardWithBattleMsg.parseFrom(packet.getBytes());
        long userId = reqMsg.getUserId();

        UserInfo userInfo = UserMgr.getUserInfo(userId);
        if (userInfo == null) {
            return;
        }
        // 哪个赛区
        int zoneType = reqMsg.getZoneType();

        // 轮次
        int round = reqMsg.getRound();
        JiuZhouRoundConfig roundConfig = config.getRoundConfigMap().get(round);
        if (roundConfig == null) {
            return;
        }

        // 什么奖励
        Property supportReward = new Property();
        if (reqMsg.hasSupportReward()) {
            supportReward = PropertyHelper.parseStringToProperty(reqMsg.getSupportReward());
        }

        String language = userInfo.getLanguage();

        // 赛场名称
        String roundName = "";
        if (roundConfig.getUpRank().get(0) == 32) {
            // 64强
            roundName = MultipleLanguageMgr.getContent("NINECHESS_SHENGYUAN_RANK_1", language);
        } else if (roundConfig.getUpRank().get(0) == 16) {
            // 32强
            roundName = MultipleLanguageMgr.getContent("NINECHESS_SHENGYUAN_RANK_2", language);
        } else if (roundConfig.getUpRank().get(0) == 8) {
            // 16强
            roundName = MultipleLanguageMgr.getContent("NINECHESS_SHENGYUAN_RANK_3", language);
        } else if (roundConfig.getUpRank().get(0) == 0) {
            // 决赛
            roundName = MultipleLanguageMgr.getContent("NINECHESS_SHENGYUAN_RANK_4", language);
        }

        String mailTitle = MultipleLanguageMgr.getContent(MailManager.NINECHESS_SUPPORT_TITLE_1, language);
        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.NINECHESS_SUPPORT_CONTENT_3, userInfo.getLanguage())
                , roundName);
        MailManager.sendMail(userId, eMailType.DAILY.getValue(), PropertyHelper.parsePropertyToString(supportReward), mailContent, mailTitle, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
    }

}
